package cn.stylefeng.guns.modular.hx.storage.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.shiro.annotion.Permission;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.hx.order.entity.HxOrderOut;
import cn.stylefeng.guns.modular.hx.order.model.params.HxOrderOutParam;
import cn.stylefeng.guns.modular.hx.order.model.result.HxOrderOutResult;
import cn.stylefeng.guns.modular.hx.storage.entity.HxStorageOffShelf;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStorageOffShelfParam;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStoragePutDetailedParam;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxStorageOffShelfResult;
import cn.stylefeng.guns.modular.hx.storage.service.HxStorageOffShelfService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 下架任务表控制器
 *
 * @author 
 * @Date 2023-09-13 17:26:11
 */
@Controller
@RequestMapping("/hxStorageOffShelf")
public class HxStorageOffShelfController extends BaseController {

    private String PREFIX = "/hx/storage/hxStorageOffShelf";

    @Autowired
    private HxStorageOffShelfService hxStorageOffShelfService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    /**
     * 跳转到主页面
     *
     * @author 
     * @Date 2023-09-13
     */
    @RequestMapping("")
    public String index(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //查询可选择仓库
        model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
/*        //查询可选择客户
        model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));*/
        return PREFIX + "/hxStorageOffShelf.html";
    }

    /**
     * 新增页面
     *
     * @author 
     * @Date 2023-09-13
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/hxStorageOffShelf_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 
     * @Date 2023-09-13
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/hxStorageOffShelf_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 
     * @Date 2023-09-13
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData add(HxStorageOffShelfParam hxStorageOffShelfParam) {
        this.hxStorageOffShelfService.add(hxStorageOffShelfParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 
     * @Date 2023-09-13
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(HxStorageOffShelfParam hxStorageOffShelfParam) {
        this.hxStorageOffShelfService.update(hxStorageOffShelfParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 
     * @Date 2023-09-13
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(HxStorageOffShelfParam hxStorageOffShelfParam) {
        this.hxStorageOffShelfService.delete(hxStorageOffShelfParam);
        return ResponseData.success();
    }

    /**
     * 批量作废
     */
    @RequestMapping("/tovoid")
    @ResponseBody
    @Permission
    public ResponseData tovoid(@RequestParam("ids") Long[] ids) {
        User user = userService.getcurrentUser();
    	for (Long id :ids) {
    		HxStorageOffShelf hxStorageOffShelf = this.hxStorageOffShelfService.getById(id);
        	if(hxStorageOffShelf.getStatus().equals("待领取")) {
        		HxStorageOffShelfParam param = new HxStorageOffShelfParam();
        		param.setTovoidTime(new Date());
        		param.setTovoidUserName(user.getName());
        		param.setStatus("作废");
        		param.setId(id);
        		hxStorageOffShelfService.update(param);
        	} else {
                return ResponseData.error("只可作废待领取任务");
        	}
		}
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 
     * @Date 2023-09-13
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(HxStorageOffShelfParam hxStorageOffShelfParam) {
        HxStorageOffShelf detail = this.hxStorageOffShelfService.getById(hxStorageOffShelfParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 
     * @Date 2023-09-13
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(HxStorageOffShelfParam hxStorageOffShelfParam,String condition ,String startTime,String endTime) {
        if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
            hxStorageOffShelfParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
        } else if (startTime == null && endTime == null){
            hxStorageOffShelfParam.setTimeInterval("'"+ DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
        }
        User user = userService.getcurrentUser();
    	hxStorageOffShelfParam.setDeptId(user.getDeptId());
    	hxStorageOffShelfParam.setOperateId(user.getOperateId());
        return this.hxStorageOffShelfService.findPageBySpec(hxStorageOffShelfParam,condition);
    }

	/**
	 * 导出
	 */
	@ResponseBody
	@RequestMapping("/listAll")
	public List<HxStorageOffShelfResult> listAll(HxStorageOffShelfParam hxStorageOffShelfParam,String condition ,String startTime,String endTime) {
        if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
            hxStorageOffShelfParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
        } else if (startTime == null && endTime == null){
            hxStorageOffShelfParam.setTimeInterval("'"+ DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
        }
        User user = userService.getcurrentUser();
    	hxStorageOffShelfParam.setDeptId(user.getDeptId());
    	hxStorageOffShelfParam.setOperateId(user.getOperateId());
		return this.hxStorageOffShelfService.findlistAllBySpec(hxStorageOffShelfParam,condition);
	}

    /**
     * 合计行
     *
     * @author 
     * @Date 2023-09-13
     */
    @RequestMapping("/count")
    @ResponseBody
    public ResponseData count(HxStorageOffShelfParam hxStorageOffShelfParam,String condition ,String startTime,String endTime) {
        if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
            hxStorageOffShelfParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
        } else if (startTime == null && endTime == null){
            hxStorageOffShelfParam.setTimeInterval("'"+ DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
        }
        User user = userService.getcurrentUser();
    	hxStorageOffShelfParam.setDeptId(user.getDeptId());
    	hxStorageOffShelfParam.setOperateId(user.getOperateId());
        return ResponseData.success(this.hxStorageOffShelfService.count(hxStorageOffShelfParam,condition));
    }

    /**
     * 下架人员信息统计
     *
     * @author lww
     * @Date 2023-12-28
     */
    @ResponseBody
    @RequestMapping("/xiajiauser")
    public LayuiPageInfo xiajiauser(HxStorageOffShelfParam hxStorageOffShelfParam , String condition, String startTime, String endTime) {

        if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
            hxStorageOffShelfParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
        } else if (startTime == null && endTime == null){
            hxStorageOffShelfParam.setTimeInterval("'"+ DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
        }
        hxStorageOffShelfParam.setStatus("已完成");
        User user = userService.getcurrentUser();
        hxStorageOffShelfParam.setDeptId(user.getDeptId());
        hxStorageOffShelfParam.setOperateId(user.getOperateId());
        return this.hxStorageOffShelfService.off(hxStorageOffShelfParam,condition);
    }


}


